/***
 * Excerpted from "Stripes: and Java Web Development is Fun Again",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/fdstr for more book information.
***/
package stripesbook.action.user;

import java.util.Date;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.EmailTypeConverter;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.ValidationErrors;
import net.sourceforge.stripes.validation.ValidationMethod;
import org.stripesbook.rayures.view.ViewTag;
import org.stripesbook.rayures.view.stripes.SButton;
import org.stripesbook.rayures.view.stripes.SForm;
import org.stripesbook.rayures.view.stripes.SHidden;
import org.stripesbook.rayures.view.stripes.SSubmit;
import org.stripesbook.rayures.view.stripes.SText;
import stripesbook.model.Contact;
import stripesbook.model.Gender;

public class ContactFormActionBean extends ContactBaseActionBean {

  @DefaultHandler
  public Resolution form() {
    return stream(getForm());
  }
  
  public Resolution save() {
    // save the contact...

  
    Contact contact = getContact();
    contact.setUser(getUser());
    contactDao.save(contact);
    contactDao.commit();

  
    getContext().getResponse().setHeader("X-Stripes-Success","true");
    return streamContactTable();
  }
  
  @ValidateNestedProperties({
    @Validate(field="firstName", maxlength=25),
    @Validate(field="lastName",  minlength=2, maxlength=40),
    @Validate(field="email", required=true, on="save",
      converter=EmailTypeConverter.class),
    @Validate(field="birthDate", expression="${this < today}")
  })
  @Override
  public void setContact(Contact contact) {
    super.setContact(contact);
  }
  @ValidationMethod(on="save")
  public void validateEmailUnique(ValidationErrors errors) {
    String email = getContact().getEmail();
    Contact other = contactDao.findByEmail(email, getUser());
    if (other != null && !other.equals(getContact())) {
      errors.add("contact.email",
        new SimpleError(bundle.get("contactEmailAlreadyUsed", other)));
    }
  }
  public Date getToday() {
    return new Date();
  }
  public Gender[] getGenders() {
    return Gender.values();
  }

  public SForm getForm() {
    return tag(SForm.class).id("form")
      .add(
        tag(ViewTag.class).tagName("div").add(tag(SHidden.class).name("contact")))
      .addInputs(
        tag(SText.class).name("contact.email").cssClass("required"),
        tag(SText.class).name("contact.firstName"),
        tag(SText.class).name("contact.lastName"),
        tag(SText.class).name("contact.phoneNumber").formatType("dashes"),
        tag(SText.class).name("contact.birthDate").formatPattern("yyyy-MM-dd")
        // tag radio buttons for gender
        /*
      <td><s:label for="contact.gender"/>:</td>
      <td class="field">
        <c:forEach var="gender" items="${actionBean.genders}">
          <s:radio name="contact.gender" value="${gender}"/>
          <fmt:message key="${gender}"/>
        </c:forEach>
      </td>
         */
      )
      .addButtons(
        tag(SSubmit.class).name("save").onclick("return submitForm(this);"),
        tag(SButton.class).name("cancel").onclick("$('#contact_form').hide();"));
  }
}
